<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // 已知字符串"aabccd"编程实现以下功能。
    // (1) 字符串去重  "abcd"
    var str = "aabccd";
    var out = "";
    for (var i = 0; i < str.length; i++) {
        // debugger;
        var zhi = str.charAt(i);
        if (out.indexOf(zhi) == -1) {
            var out = out.concat("", zhi);
        }
    }
    document.write(out + "<br>");

    // (2) 统计每个字符出现的次数，结果显示   a2b1c2d1     字符统计(每个字符出现的总次数)
    var str_two = "";
    for (var i = 0; i < out.length; i++) {
        var sum = 0;
        for (var j = 0; j < str.length; j++) {
            if (out.charAt(i) == str.charAt(j)) {
                sum++;
            }
        }
        str_two += out.charAt(i) + sum;
    }
    document.write(str_two + "<br>");



    //(3)(原始字符串：aaaabbbbbbbccdaaaaa；压缩后：a4b7c2d1a5)  压缩统计 (相邻字符重复的次数)

    var str_three = "aaaabbbbbbbccdaaaaa";
    var out_four = ""
    var count = 0;
    for (var i = 0 ; i < str_three.length ; i++) {
        if (str_three.charAt(i) == str_three.charAt(i+1)) {
            count++;
        } else {
            count++;
            out_four += str_three.charAt(i) + count;
            count = 0;
        }
    }

    document.write(out_four + "<br>");
    document.write(str_four + "<br>");
</script>

</html>